stringFuncs.removeOnce   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
/** global: UB */
2
3
var stringFuncs = {
4
	
5
	removeAllCI: function(find, replace){
6
		var s = this.toString();
7
		return s.removeAll(find, replace, false, false);
8
	},
9
	removeAll: function(find, wholeWords = false, caseSensitive = true){
10
		var s = this.toString();
11
		
12
		if (find.isArray()){
13
			for (var r = 0, rl = find.length; r < rl; r++) {
14
				s = s.removeAll(find[r], wholeWords, caseSensitive);
15
			}
16
			return s;
17
		}else{
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
18
			return s.replaceAll(find, "", wholeWords, caseSensitive);
19
		}
20
	},
21
	removeBetween: function(findStart, findEnd, keepEnds = true, startAt = 0, times = 0){
22
		var s = this.toString();
23
		return s.replaceBetween(findStart, findEnd, "", keepEnds, startAt, times);
24
	},
25
	removeOnce: function(find, wholeWords = false, caseSensitive = true){
26
		var s = this.toString();
27
		return s.replaceOnce(find, "", wholeWords, caseSensitive);
28
	},
29
	
30
	removePrefix: function(prefix, caseSensitive = true){
31
		var str = this.toString();
32
		
33
		// remove all the given prefixes
34
		if (prefix.isArray()){
35
			for (var r = 0, rl = prefixes.length; r < rl; r++) {
0 ignored issues
show
Bug introduced by
The variable prefixes seems to be never declared. If this is a global, consider adding a /** global: prefixes */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
36
				var prefix = prefixes[r];
37
				str = str.removePrefix(prefix, caseSensitive);
38
			}
39
		}else{
40
41
			// some quick checks
42
			if (prefix && prefix.length > 0 && str.length >= prefix.length && str.charCodeAt(0) == prefix.charCodeAt(0)) {
43
				
44
				// extract prefix
45
				var p = str.substr(0, prefix.length);
46
				
47
				// compare case insensitively if wanted
48
				if (!caseSensitive) {
49
					p = p.toLowerCase();
50
					prefix = prefix.toLowerCase();
51
				}
52
				
53
				// compare
54
				if (p == prefix) {
55
					
56
					// remove prefix
57
					return str.substring(prefix.length);
58
				}
59
			}
60
		}
61
		return str;
62
	},
63
	removePostfix: function(postfix, caseSensitive = true){
64
		var str = this.toString();
65
		
66
		// remove all the given postfixes
67
		if (postfix.isArray()){
68
			for (var r = 0, rl = postfixes.length; r < rl; r++) {
0 ignored issues
show
Bug introduced by
The variable postfixes seems to be never declared. If this is a global, consider adding a /** global: postfixes */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
69
				var postfix = postfixes[r];
70
				str = str.removePostfix(postfix, caseSensitive);
71
			}
72
		}else{
73
74
			// some quick checks
75
			if (postfix && postfix.length > 0 && str.length >= postfix.length && str.charCodeAt(str.length - 1) == postfix.charCodeAt(postfix.length - 1)) {
76
				
77
				// extract postfix
78
				var p = str.substr(str.length - postfix.length);
79
				
80
				// compare case insensitively if wanted
81
				if (!caseSensitive) {
82
					p = p.toLowerCase();
83
					postfix = postfix.toLowerCase();
84
				}
85
				
86
				// compare
87
				if (p == postfix) {
88
					
89
					// remove postfix
90
					return str.substr(0, str.length - p.length);
91
				}
92
				
93
			}
94
		}
95
		return str;
96
	},
97
	
98
	removeEnding: function(chars){
99
		var str = this.toString();
100
		if (chars >= str.length) {
101
			return "";
102
		}
103
		return str.substring(0, str.length - chars);
104
	},
105
	removeStarting: function(chars){
106
		var str = this.toString();
107
		if (chars >= str.length) {
108
			return "";
109
		}
110
		return str.substring(chars, str.length);
111
	},
112
	none:null
113
};
114
115
// register funcs
116
UB.registerFuncs(String.prototype, stringFuncs);